<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class BluetoothController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	public function index(){
		$Bluetooth = D('Bluetooth');
		if(IS_GET){
			$blue_id = I('blue_id') ? (int)I('blue_id') : 0;
			if($blue_id){
				$info = $Bluetooth->getBluetoothInfoById($blue_id);
				$this->assign('info', $info);
				$this->display('add');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = $Bluetooth->getBluetoothListCount();
				//用户列表
				$list = $Bluetooth->getBluetoothListByWhere($page, array());
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				
				//判断请求方式
				if(!$ajax){
					$this->display('index');
				}else{
					$this->display('blue_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'blue_name' => I('post.blue_name', ''),
				'blue_nick' => I('post.blue_nick', ''),
				'updatetime' => time(),
				'admin_id' => session('user_id'),
				'admin_nick' => session('nickname')
			);
			
			$blue_id = I('post.blue_id') ? (int)I('post.blue_id') : 0;
			if(empty($data['blue_name'])){
				$this->ajaxReturn('请输入设备名');
			}else if(empty($data['blue_nick'])){
				$this->ajaxReturn('请输入检测点名');
			}else{
				if($blue_id){
					$row = $Bluetooth->editBluetoothInfoById($blue_id, $data);
				}else{
					$row = $Bluetooth->addBluetoothInfo($data);
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	public function add(){
		$this->display();
	}
	
	public function insp(){
		$Bluetooth = D('Bluetooth');
		if(IS_GET){
			$insp_id = I('insp_id') ? (int)I('insp_id') : 0;
			if($insp_id){
				$info = $Bluetooth->getInspectingInfoById($insp_id);
				$this->assign('info', $info);
				$this->display('add');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = $Bluetooth->getInspectingListCountByWhere();
				//用户列表
				$list = $Bluetooth->getInspectingListByWhere($page, array());
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				
				//判断请求方式
				if(!$ajax){
					$this->display('insp');
				}else{
					$this->display('insp_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'udid' => I('post.udid', ''),
				'nickname' => I('post.nickname', ''),
				'updatetime' => time(),
				'admin_id' => session('user_id'),
				'admin_nick' => session('nickname')
			);
			
			$insp_id = I('post.insp_id') ? (int)I('post.insp_id') : 0;
			if(empty($data['udid'])){
				$this->ajaxReturn('请输入手机唯一标识');
			}else if(empty($data['nickname'])){
				$this->ajaxReturn('请输入巡检员名称');
			}else{
				if($insp_id){
					$row = $Bluetooth->editInspectingInfo($insp_id, $data);
				}else{
					$row = $Bluetooth->addInspectingInfo($data);
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	public function insp_add(){
		$this->display();
	}
	
	public function logs(){
		$Bluetooth = D('Bluetooth');
		$page = I('get.page') ? (int)I('get.page') : 1;
		$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
		$nickname = I('get.nickname', '');
		//获取条件
		if(!empty($nickname)){
			$where['nickname'] = array('like', '%'. $nickname .'%');
			$param['nickname'] = $nickname;
		}
		//巡检点
		$blue_id = I('blue_id', 0);
		if($blue_id){
			$where['blue_id'] = $blue_id;
			$param['blue_id'] = $blue_id;
		}
		//时间
		$times = I('times', '');
		if(!empty($times)){
			$param['times'] = $times;
			$where['createtime'] = array('between', array(strtotime($times. ' 00:00:00'), strtotime($times.' 23:59:59')));
		}
		
		$count = $Bluetooth->getLogsListCountByWhere($where);
		//用户列表
		$list = $Bluetooth->getLogsListByWhere($page, $where);
		
		$this->assign('list', $list);
		$this->assign('count', $count);
		$this->assign('page', $page);
		if(count($param)){
			$this->assign('param', json_encode($param));
		}
		
		//判断请求方式
		if(!$ajax){
			$bluelist = $Bluetooth->getAllBluetooth();
			$this->assign('bluelist', $bluelist);
			$this->display('logs');
		}else{
			$this->display('logs_list', 'utf-8', 'html');
		}
	}
}


   